1665D - GCD Guess - CodeForces Solution


bitmasks chinese remainder theorem constructive algorithms interactive math number theory

Please click on ads to support us..

Python Code:

import sys
import random
from bisect import bisect_left as lb
from bisect import bisect_right as rb
from collections import deque
from queue import PriorityQueue as pq
from math import gcd
input_ = lambda: sys.stdin.readline().strip("\r\n")
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
lii = lambda : list(map(int, list(ip())))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab,bc,cd : ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x : print(x)
prinT = lambda x : print(x)
f = lambda : sys.stdout.flush()
inv =lambda x:pow(x,mod-2,mod)
dx = [0,0,1,-1]
dy = [1,-1,0,0]
mod = 10**9 + 7
mod1 = 998244353

for _ in range (ii()) :
    x = 0

    for i in range (30) :
        print('?',(1<<i) - x,(3<<i) - x)
        f()
        t = ii()

        if ((t>>i)&1 == 0) :
            x += (1<<i)
        
    print('!',x)
    f()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
const ll M = 1e9 + 7;

/*
        ***        **************
        ***        **************
        ***        ***
        ***        ***
        ***        ***
        *************************
        *************************
                   ***        ***
                   ***        ***
                   ***        ***
        **************        ***
        **************        ***

*/

#define v(li) vector<ll>
#define vp(li) vector<pair<ll, ll>>
#define m(li) map<ll, ll>
#define s(li) set<ll>

#define f(i, n) for (ll i = 0; i < n; i++)
#define f1(i, n) for (ll i = 1; i < n+1; i++)
#define test(t) while (t--)
#define w(i, n) while (i < n)
#define prs(n) cout << n << " "
#define prn(n) cout << n << " "
#define newl cout << "\n"
#define pb(a) push_back(a);
#define pop pop_back()
#define in(x) insert(x)
#define fsort(v) sort(v.begin(), v.end())
#define rsort(v) sort(v.begin(), v.end(), greater<ll>())
#define rev(v) reverse(v.begin(), v.end())
#define max_ele(v) *max_element(v.begin(), v.end())
#define min_ele(v) *min_element(v.begin(), v.end())

ll binexp(ll a, ll b)
{
    if (b == 0)
        return 1;
    ll res = binexp(a, b / 2);
    if (b % 2 == 0)
        return res * res;
    else
        return a * res * res;
}

ll biexm(ll a, ll b)
{
    if (b == 0)
        return 1;
    ll res = biexm(a, b / 2);
    if (b % 2 == 0)
        return ((res % M) * (res % M)) % M;
    else
        return ((((res % M) * (res % M)) % M) * (a % M)) % M;
}

ll hcf(ll a, ll b)
{
    if (a % b == 0)
        return b;
    return hcf(b, a % b);
}
ll lcm(ll a,ll b){
    return (a*b)/(hcf(a,b));
}

int main()
{   
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    /* Sieve */
    // ll N=1e6;
    // ll spf[N+1];
    // f(i,N+1) spf[i]=i;
    // for(ll i=2;i<N+1;i++){
    //     if(spf[i]==i){
    //         for(ll j=i*i;j<N+1;j+=i){
    //            spf[j]=i;
    //         }
    //     }
    // }


    // ll N=1e6;
    // bool isprime[N+1];
    // f1(i,N) isprime[i]=true;
    // isprime[1]=false;
    // for(ll i=2;i<N+1;i++){
    //     if(isprime[i]){
    //         for(ll j=i*i;j<N+1;j+=i) isprime[j]=false;
    //     }
    // }


    // vector<ll> primes;
    // for(ll i=2;i<N+1;i++) if(isprime[i]) primes.push_back(i);


            /* Code Starts here */
    
    ll t;
    cin >> t;
    test(t)
    {   
       ll b=(1LL<<30);
       ll num=0;
       for(ll i=0;i<30;i++){
            ll r=(1LL<<i)-num;
            cout<<"? "<<r<<" "<<b+r<<"\n";
            ll q=(1LL<<i);
            cout.flush();
            ll x;
            cin>>x;
            if(x>q) num+=q;
       }

       cout<<"! "<<num<<"\n";
       cout.flush();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves